﻿using Bll;
using Model;
using System;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using System.Text;
using System.Web;
using System.Web.Mvc;
using Web.Models;

namespace Web.Controllers
{
    public class CustomerController3 : Controller
    {
        public ActionResult List2()
        {
            var bll = new CustomerBll();
            var list = bll.GetAll();
            var model = new CustomerModel();
            model.list = list;
            return View(model);
        }

        public ActionResult List()
        {
            var bll = new TableBll();
            string tblName = "Customer";
            string strWhere = "";
            int pageIndex = 1;
            Model_MakePages page = new Model_MakePages(tblName, strWhere, pageIndex);
            PageTable pageTable = bll.GetData_Page_FldsOrder(page);

            return View(pageTable);
        }

        [HttpPost]
        public string GetCustomer(string strWhere, int pageIndex)
        {
            var bll = new TableBll();
            string tblName = "Customer";
            Model_MakePages page = new Model_MakePages(tblName, strWhere, pageIndex);
            PageTable pageTable = bll.GetData_Page_FldsOrder(page);
            string str = Tool.SerializeObject(pageTable);

            return str;
        }
        //[HttpPost]
        //public ActionResult GetCustomer(string strWhere = "", int pageIndex = 0)
        //{
        //    var bll = new TableBll();
        //    string tblName = "Customer";
        //    Model_MakePages page = new Model_MakePages(tblName, strWhere, pageIndex);
        //    PageTable pageTable = bll.GetData_Page_FldsOrder(page);

        //    var res = new JsonResult();
        //    res.Data = pageTable;
        //    res.JsonRequestBehavior = JsonRequestBehavior.AllowGet;//允许使用GET方式获取，否则用GET获取是会报错。
        //    return res;
        //}

        public ActionResult Edit(int id = 0)
        {
            if (id == 0)
            {
                var model = new Customer();
                return View(model);
            }
            else
            {
                var model = new CustomerBll().GetById(id);
                return View(model);
            }
        }

        [HttpPost]
        public ActionResult Edit(Customer model)
        {
            var bll = new CustomerBll();
            if (bll.Add(model) > 0)
            {
                ViewBag.EditResult = "<script>alert('保存成功');document.location.href='List'</script>";
            }
            else
            {
                ViewBag.EditResult = "<script>alert('保存失败，请稍后重试！');</script>";
            }

            return View(model);
        }
    }
}
